import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../common/flatPanel.dart';
import '../entry.dart';

class LoginPage extends StatefulWidget {
  LoginPage({Key key, this.title: Entry.Login}) : super(key: key);

  final String title;

  @override
  _LoginPageState createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  final _userName = TextEditingController();
  final _userPwd = TextEditingController();
  final GlobalKey _formKey = new GlobalKey<FormState>();

  void _login() {
    if (_userName.text == "admin" && _userPwd.text == "123456") {
      _saveLoginMsg();
      Navigator.pushNamed(context, "index_page");
    } else {
      showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(content: Text('登录失败，账号或密码错误\n点击空白处继续'));
          });
    }
  }

  // 保存账号密码
  void _saveLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setString("name", _userName.text);
    preferences.setString("pwd", _userPwd.text);
  }

  // 读取账号密码，并将值直接赋给账号框和密码框
  void _getLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    _userName.text = preferences.get("name");
    _userPwd.text = preferences.get("pwd");
  }

  @override
  void initState() {
    super.initState();
    _getLoginMsg();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: FlatPanel(
        child: Form(
          key: _formKey,
          autovalidateMode: AutovalidateMode.onUserInteraction,
          child: Column(
            children: <Widget>[
              TextFormField(
                autofocus: true,
                keyboardType: TextInputType.text,
                controller: _userName,
                decoration: InputDecoration(
                    labelText: '用户名',
                    hintText: '用户名',
                    prefixIcon: Icon(Icons.person)),
                validator: (v) => v.trim().length > 0 ? null : '用户名不能为空！',
              ),
              TextFormField(
                controller: _userPwd,
                keyboardType: TextInputType.number,
                obscureText: true,
                decoration: InputDecoration(
                    labelText: '密码',
                    hintText: '密码',
                    prefixIcon: Icon(Icons.lock)),
                validator: (v) => v.trim().length > 3 ? null : '密码最少3位！',
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: RaisedButton(
                        padding: EdgeInsets.all(16),
                        child: Text('清空', style: TextStyle(fontSize: 24)),
                        onPressed: () =>
                            (_formKey.currentState as FormState).reset()),
                  ),
                  Expanded(
                      flex: 1,
                      child: RaisedButton(
                        padding: EdgeInsets.all(16),
                        child: Text('提交', style: TextStyle(fontSize: 24)),
                        onPressed: _login,
                      )),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
